﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using MySqlX.XDevAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird.OAWeb.WebAPI.Config
{
    public static class SwaggerConfig
    {
        public static void Swaggersetup(this IServiceCollection services)
        {
            #region
            //        services.AddSwaggerGen( x => {
            //        var basepath = AppContext.BaseDirectory;
            //        var xmlpath = System.IO.Path.Combine(basepath, "Bird.OAWeb.WebAPI.xml");
            //        x.IncludeXmlComments(xmlpath,true);
            //        x.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
            //        {
            //            Title = "Yuan",
            //            Version = "v1",
            //            Contact = new Microsoft.OpenApi.Models.OpenApiContact
            //            {
            //                Name = "Yuan",
            //                Email = "2515416581@qq.com",
            //            }
            //        });
            //    }
            //);

            //    //return services;
            #endregion
            #region
            //services.AddSwaggerGen(c =>
            //{
            //    var basePath = AppContext.BaseDirectory; //获取应用程序的所在目录
            //                                             //或者用下面的方式也能获取
            //    var basePath2 = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            //    var xmlPath = System.IO.Path.Combine(basePath, "Bird.OAWeb.WebAPI.xml"); //拼接XML文件所在路径
            //                                                                             //让Swagger显示方法、类的XML注释信息
            //    c.IncludeXmlComments(xmlPath, true);
            //    //设置Swagger文档参数
            //    c.SwaggerDoc("v1", new OpenApiInfo
            //    {
            //        Title = "Yuan",
            //        Version = "v1",
            //        Description = "Asp.Net Core5 WebApi开发实战", //描述信息
            //        Contact = new OpenApiContact() //开发者信息
            //        {
            //            Name = "Yuan", //开发者姓名
            //            Email = "2515416581@qq.com", //email地址
            //            Url = new Uri("https://gitee.com/xuerenwang") //作者的主页网站
            //        }
            //    });
            //    //开启Authorize权限按钮
            //    c.AddSecurityDefinition("JWTBearer", new OpenApiSecurityScheme()
            //    {
            //        Description = "这是方式一(直接在输入框中输入认证信息，不需要在开头添加Bearer) ",
            //        Name = "Authorization", //jwt默认的参数名称
            //        In = ParameterLocation.Header, //jwt默认存放Authorization信息的位置(请求头中)
            //        Type = SecuritySchemeType.Http,
            //        Scheme = "Bearer"
            //    });
            //});
            #endregion
            #region  
            services.AddSwaggerGen(c =>
            {
                //Swagger开发者信息
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "Bird.OAWeb",
                    Version = "v1",
                    Description = "Asp.Net Core6 WebApi开发实战",
                    Contact = new Microsoft.OpenApi.Models.OpenApiContact()
                    {
                        Name = "Yuan",
                        Email = "2515416581@qq.com",
                        Url = new Uri("https://gitee.com/liuyiyly")
                    }
                });
                //开启Authorize权限按钮
                c.AddSecurityDefinition("JWTBearer", new OpenApiSecurityScheme()
                {
                    Description = "这是方式一(直接在输入框中输入认证信息，不需要在开头添加Bearer) ",
                    Name = "Authorization", //jwt默认的参数名称
                    In = ParameterLocation.Header, //jwt默认存放Authorization信息的位置(请求头中)
                    Type = SecuritySchemeType.Http,
                    Scheme = "Bearer"
                 });
            //读取这个文件里面的注释信息，显示在swagger界面上 中文注释
            //定义变量获取文件路径地址  AppDomain.CurrentDomain.BaseDirectory 动态获取地址路径
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "Bird.OAWeb.WebAPI.xml";
                c.IncludeXmlComments(xmlPath, true);
                //声明一个Scheme，注意下面的Id要和上面AddSecurityDefinition中的参数name一致
                var scheme = new OpenApiSecurityScheme
                {
                    Reference = new OpenApiReference()
                    {
                        Id = "JWTBearer", //这个名字与上面的一样
                        Type = ReferenceType.SecurityScheme
                    }
                };
                //注册全局认证（所有的接口都可以使用认证）
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { scheme, Array.Empty<string>() }
                });
            });
            
            #endregion
        }
    }
}